﻿using System;
using System.Web;
using System.Data;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using BrnMall.Core;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Web.StoreAdmin.Models;
using BrnMall.Manager.Manager.User;
using BrnMall.Manager.Manager.Store;
using BrnMall.Manager.Domain.User;

namespace BrnMall.Web.StoreAdmin.Controllers
{
    public partial class CashItemController : BaseStoreAdminController
    {
        /// <summary>
        /// 赠送选项列表
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult List(int pageNumber = 1, int pageSize = 15)
        {
            var listModel = CashItemManager.GetListPage(WorkContext.StoreId, pageNumber, pageSize);
            var model = new Models.CashItemListModel();

            model.PageModel = new PageModel(pageSize, pageNumber, (int)listModel.TotalItems);
            model.List = listModel.Items;
            return View(model);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(CashItem model)
        {
            model.state = 1;
            model.StoreId = WorkContext.StoreId;
            model.CreateTime = DateTime.Now;

            if (model.Amount < 0)
                ModelState.AddModelError("Amount", "充值金额必须大于0");
            if (model.GiveAmount < 0)
                ModelState.AddModelError("GiveAmount", "赠送金额必须大于0");

            CashItemManager.Add(model);
            return PromptView("添加成功");
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var model = CashItemManager.Get(id, 0);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(CashItem model)
        {
            model.StoreId = WorkContext.StoreId;
            model.CreateTime = DateTime.Now;

            CashItemManager.Update(model);

            return PromptView("修改成功");
        }
        
        public ActionResult Delete(int id)
        {

            CashItemManager.Delete(id, WorkContext.StoreId);

            return PromptView("删除成功");
        }

    }
}